home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / misc / kidsmenu.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  6KB  |  269 lines

  1. /*
  2.  * KIDS MENU
  3.  *
  4.  * Copyright 1993-1994 Dave Dunfield
  5.  *
  6.  * Permission granted for personal (non-commercial) use only.
  7.  *
  8.  * Compile command: cc kmedit -fop
  9.  */
  10. #include <stdio.h>
  11. #include <window.h>
  12.  
  13. #define    MAXPACK    27
  14.  
  15. /* Termination codes */
  16. #define    BATCH    50
  17. #define    EXIT    51
  18. #define    BADARG    52
  19.  
  20. /* Mouse buttons */
  21. #define    MOUSE_LEFT        0x01    /* Select button */
  22. #define    MOUSE_RIGHT        0x02    /* Cancel button */
  23. #define    MOUSE_CENTER    0x04    /* Not used in MICROCAD */
  24.  
  25. int mousex = 0, mousey = 0, max_icon;
  26. char cursor_flag = 0, video_mode = 3, mouse_flag = -1, quit_flag = -1,
  27.     *ifile = 0, *ofile = 0;
  28.  
  29. unsigned char itext[MAXPACK][8][6], iattr[MAXPACK][8][6],
  30.     idir[MAXPACK][50], icmd[MAXPACK][50];
  31.  
  32. char htext[] = {
  33. "KIDS MENU - Copyright 1993-1994 Dave Dunfield, Freely Distributable.\n\n\
  34. Use: KIDSMENU [B=batch_file D=data_file -Lock -Mouse V=video_mode]\n\n\
  35. for Bethany and Michael.\n" };
  36.  
  37. /*
  38.  * Main program - process commands at the highest level
  39.  */
  40. main(argc, argv)
  41.     int argc;
  42.     char *argv[];
  43. {
  44.     unsigned i, x, y, inkey;
  45.     char *ptr;
  46.     FILE *fp;
  47.  
  48.     for(i=1; i < argc; ++i) {
  49.         ptr = argv[i];
  50.         switch((toupper(*ptr++) << 8) | toupper(*ptr++)) {
  51.             default:
  52.                 printf("Invalid argument: %s\n\n", argv[i]);
  53.             case '?'<<8 :
  54.             case '/?' :
  55.             case '-?' :
  56.                 fputs(htext, stdout);
  57.                 exit(BADARG);
  58.             case 'V=' :
  59.                 video_mode = atoi(ptr);
  60.                 break;
  61.             case 'D=' :
  62.                 ifile = ptr;
  63.                 break;
  64.             case '-M' :
  65.                 mouse_flag = 0;
  66.                 break;
  67.             case '-L' :
  68.                 quit_flag = 0;
  69.                 break;
  70.             case 'B=' :
  71.                 get_name(icmd, ptr, ".BAT");
  72.                 ofile = fopen(icmd, "wvq"); } }
  73.  
  74.  
  75.     get_name(icmd, ifile || "KIDSMENU", ".DAT");
  76.     fp = fopen(icmd, "rvqb");
  77.  
  78.     max_icon = 0;
  79.     do {
  80.         i =  fread(itext[max_icon], sizeof(itext[0]), fp);
  81.         i += fread(iattr[max_icon], sizeof(iattr[0]), fp);
  82.         i += fread(idir [max_icon], sizeof(idir [0]), fp);
  83.         i += fread(icmd [max_icon], sizeof(icmd [0]), fp);
  84.         ++max_icon; }
  85.     while(i == (sizeof(itext[0])+sizeof(iattr[0])+sizeof(idir[0])+sizeof(icmd[0])));
  86.     --max_icon;
  87.     fclose(fp);
  88.  
  89. runmenu:
  90.     set_video_mode(video_mode);
  91.     wopen(0, 0, 80, 25, WCOPEN|WCCLOSE|NORMAL);
  92.     W_OPEN->WINpcurx = W_OPEN->WINpcury = inkey = 0;
  93.     if(mouse_flag = mouse_flag && init_mouse())
  94.         wcursor_block();
  95.     else
  96.         wcursor_off();
  97.  
  98.     /* Draw the ICONs */
  99.     for(i=x=y=0; i < max_icon; ++i) {
  100.         draw_icon(x, y, itext[i], iattr[i]);
  101.         *W_OPEN = NORMAL;
  102.         wgotoxy(x+3, y+6);
  103.         wprintf("%2u", i+1);
  104.         if((x += 9) > 79) {
  105.             x = 0;
  106.             y += 8; } }
  107.  
  108. again:
  109.     while(!(mouse_status() & MOUSE_LEFT)) switch(i = wtstc()) {
  110.         case _CEN :
  111.             if(quit_flag) {
  112.                 *W_OPEN = NORMAL;
  113.                 wclose();
  114.                 fputs(htext, stdout);
  115.                 exit(EXIT); }
  116.             break;
  117.         case '0' :
  118.         case '1' :
  119.         case '2' :
  120.         case '3' :
  121.         case '4' :
  122.         case '5' :
  123.         case '6' :
  124.         case '7' :
  125.         case '8' :
  126.         case '9' :
  127.             inkey = ((inkey % 10)*10) + (i - '0');
  128.             goto xdraw;
  129.         case _KBS :
  130.             inkey /= 10;
  131.         xdraw:
  132.             if(inkey > max_icon) {
  133.                 inkey = 0;
  134.                 wputc(7); }
  135.             wgotoxy(76, 24);
  136.             wprintf(inkey ? "%-2u" : "  ", inkey);
  137.             mousex = mousey = -1;
  138.             break;
  139.         case '\n' :
  140.             if(inkey) {
  141.                 i = inkey - 1;
  142.                 goto runpgm; } }
  143.  
  144.     if(((mousey % 8) >= 6) || ((mousex % 9) == 8))
  145.         goto again;
  146.     if((i = ((mousey/8)*9) + mousex/9) >= max_icon)
  147.         goto again;
  148.  
  149. runpgm:    
  150.     *W_OPEN = NORMAL;
  151.     wclose();
  152.  
  153.     if(ofile) {
  154.         fprintf(ofile,"CD %s\n", idir[i]);
  155.         fprintf(ofile,"%s\n", icmd[i]);
  156.         fclose(ofile);
  157.         exit(BATCH); }
  158.  
  159.     cd(idir[i]);
  160.     system(icmd[i]);
  161.     goto runmenu;
  162. }
  163.  
  164. /*
  165.  * Display the ICON
  166.  */
  167. draw_icon(x, y, text, attr)
  168.     unsigned x, y;
  169.     unsigned char text[8][6], attr[8][6];
  170. {
  171.     int x1, y1;
  172.  
  173.     for(y1=0; y1 < 6; ++y1) {
  174.         wgotoxy(x, y+y1);
  175.         for(x1 = 0; x1 < 8; ++x1) {
  176.             *W_OPEN = attr[x1][y1];
  177.             wputc((unsigned)text[x1][y1] | 0x0100); } }
  178. }
  179.  
  180. /*
  181.  * Get a filename with extension only if necessary
  182.  */
  183. get_name(dest, name, ext)
  184.     char *dest, *name, *ext;
  185. {
  186.     char c, flag;
  187.  
  188.     flag = -1;
  189.     while(*dest = c = *name++) {
  190.         ++dest;
  191.         if(c == '.')
  192.             flag = 0; }
  193.  
  194.     if(flag)
  195.         while(*dest++ = *ext++);
  196. }
  197.  
  198. /*
  199.  * Initializes the mouse driver, returns with -1 if successful.
  200.  */
  201. init_mouse() asm
  202. {
  203. ; Initialize & test for mouse
  204.         XOR        AX,AX            ; Init functions.
  205.         INT        33h                ; Call mouse driver
  206.         AND        AX,AX            ; Mouse present
  207.         JZ        initm1            ; No, skip it
  208. ; Set mouse limits (some drivers do not do it properly on reset)
  209.         XOR        CX,CX            ; Lower limit is zero
  210.         MOV        DX,799            ; Upper horizontal limit
  211.         MOV        AX,7            ; Set horizontal limit
  212.         INT        33h                ; Call mouse driver
  213.         MOV        DX,499            ; Upper vertical limit
  214.         MOV        AX,8            ; Set vertical limit
  215.         INT        33h                ; Call mouse driver
  216.         MOV        AX,-1            ; Indicate mouse ok
  217. initm1:
  218. }
  219.  
  220. /*
  221.  * Update mouse position and on-screen cursor. If any button is
  222.  * activated, remove cursor, wait for button to be released, and
  223.  * report it.
  224.  */
  225. mouse_status()
  226. {
  227.     int x, y, z;
  228.  
  229.     if(!mouse_flag)
  230.         return 0;
  231.  
  232.     /* Get mouse position and button status */
  233.     asm {
  234.         MOV        AX,0003h    ; Mouse status function
  235.         INT        33h            ; Call mouse driver
  236.         MOV        -2[BP],BX    ; Save buttons
  237.         MOV        -4[BP],DX    ; Save Y position
  238.         MOV        -6[BP],CX    ; Save X position
  239.         }
  240.  
  241.     x /= 10;        /* Get new X */
  242.     y /= 20;        /* Get new Y */
  243.     /* If cursor position changed, update cursor and display*/
  244.     if((x != mousex) || (y != mousey))
  245.         wgotoxy(mousex = x, mousey = y);
  246.  
  247.     /* If any buttons are activated, wait for release, remove cursor */
  248.     if(z & (MOUSE_LEFT|MOUSE_RIGHT|MOUSE_CENTER)) asm
  249.     {
  250.         mloop1:    MOV        AX,0003h        ; Mouse status function
  251.                 INT        33h                ; Call mouse driver
  252.                 AND        BL,07h            ; Any buttons down?
  253.                 JNZ        mloop1            ; Wait till clear
  254.     }
  255.  
  256.     /* Pass back button status to caller */
  257.     return z;
  258. }
  259.  
  260. /*
  261.  * Set video mode
  262.  */
  263. set_video_mode(x) asm
  264. {
  265.         MOV        AL,4[BP]    ; Get mode
  266.         XOR        AH,AH        ; Set mode fucntion
  267.         INT        10h            ; Call BIOS
  268. }
  269.